package DAO.impl.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import DAO.IMessageDAO;

import domain.Message;

public class MessageDAOImpl extends JdbcDAO implements IMessageDAO {

	private static Logger log = Logger.getLogger(MessageDAOImpl.class);
	
	@Override
	public int createMessage(Message message) {

		int result = 0;
		Connection con = this.GetConnection();
		if (con != null) {

			String formatsql = "INSERT INTO messages (id, createdate, msg, room, \"user\") VALUES (?,?,?,?,?)";
			PreparedStatement st;
			try {

				st = con.prepareStatement(formatsql);
//				st.setInt(1, message.getId());
//				st.setString(2, message.getCreateDate());
//				st.setString(3, message.getMsg());
//				st.setString(4, message.getRoom());
//				st.setString(5, message.getUser());

				result = st.executeUpdate();

				con.close();
			} catch (SQLException e) {
				log.error(e);
			}

		}

		return result;
	}

	@Override
	public int updateMessage(Message message) {

		int result = 0;
		Connection con = this.GetConnection();
		if (con != null) {

			String formatsql = "update messages set createdate=?, msg=?, room=?, \"user\"=? where id=?";
			PreparedStatement st;
			try {

				st = con.prepareStatement(formatsql);
//				st.setString(1, message.getCreateDate());
//				st.setString(2, message.getMsg());
//				st.setString(3, message.getRoom());
//				st.setString(4, message.getUser());
//				st.setInt(5, message.getId());
				
				result = st.executeUpdate();

				con.close();
			} catch (SQLException e) {
				log.error(e);
			}

		}

		return result;
	}

	@Override
	public int deleteMessage(int id) {

		int result = 0;
		Connection con = this.GetConnection();
		if (con != null) {

			String formatsql = "delete from messages where id=?";
			PreparedStatement st;
			try {

				st = con.prepareStatement(formatsql);
				st.setInt(1, id);

				result = st.executeUpdate();

				con.close();
			} catch (SQLException e) {
				log.error(e);
			}

		}

		return result;

	}

	@Override
	public Message getMessage(int id) {

		Message message = null;
		Connection con = this.GetConnection();
		if (con != null) {

			String formatsql = "select * from messages where id=?";
			PreparedStatement st;
			try {

				st = con.prepareStatement(formatsql);
				st.setInt(1, id);

				ResultSet resultSet = st.executeQuery();
				if (resultSet.next()) {
					message = new Message();
//					message.setId(resultSet.getInt("id"));
//					message.setCreateDate(resultSet.getString("createdate"));
//					message.setMsg(resultSet.getString("msg"));
//					message.setRoom(resultSet.getString("room"));
					//message.setUser(resultSet.getString("user"));
				}

				con.close();
			} catch (SQLException e) {
				log.error(e);
			}

		}

		return message;

	}

	@Override
	public List<Message> getAllMessages() {

		List<Message> messages = new ArrayList<Message>();

		
		Connection con = this.GetConnection();
		if (con != null) {

			String formatsql = "select * from messages";
			PreparedStatement st;
			try {

				
				st = con.prepareStatement(formatsql);

				ResultSet resultSet = st.executeQuery();

				while (resultSet.next()) {
					Message message = new Message();
//					message.setId(resultSet.getInt("id"));
//					message.setCreateDate(resultSet.getString("createdate"));
//					message.setMsg(resultSet.getString("msg"));
//					message.setRoom(resultSet.getString("room"));
//					message.setUser(resultSet.getString("user"));

					messages.add(message);
				}

				con.close();
			} catch (SQLException e) {
				log.error(e);
			}

		}
		

		return messages;

	}

}
